<?php
class NewsController extends AppController
{
	public $name = 'News';


	public $paginate = array(
        'limit' => 10,
        'order' => array(
            'News.priority' => 'desc',
            'News.id' => 'desc'
        ),
        'contain' => array('News')
    );

	public function Index()
	{
        $newsList = $this->getNewss();
        $this->set('listData', $newsList);

        if ($this->request->is('ajax')) {
            $this->disableCache();
            $this->layout = false;
            $this->autoRender = false;
            $this->render('/Elements/contentlist');
        } else {
            App::uses('Category', 'Model');
            $category = new Category();
            $tabList = $category->find('list');

            $this->set('tabList', $tabList);
        }
	}

    public function View($id = null)
    {
        $this->paginate['limit'] = 1;
        if ($id == null) {
            $this->redirect('/');
        }

        $data = $this->paginate('News', array(
            'News.id' => $id,
        ));
        if (!empty($data)) {
            $this->_setNextNewsInfo($data[0]);
            $this->_setPrevNewsInfo($data[0]);

            App::uses('Category', 'Model');
            $category = new Category();
            $tabList = $category->find('list');

            $this->set('tabList', $tabList);
            $this->set('dataDetail', $data[0]);
        } else {
            $this->redirect('/');
        }

    }

    private function _setNextNewsInfo($currentData)
    {
        $caseType = $this->_getNewsType();
        $conditions = array();
        if ($caseType != 0) {
            $conditions['News.news_category_id'] =  $caseType;
        
        }
        $conditions['News.id <'] = $currentData['News']['id'];
        $data = $this->paginate('News', $conditions);
        $this->set('nextDetail', isset($data[0])?$data[0]:array());
    }

    private function _setPrevNewsInfo($currentData)
    {
        $caseType = $this->_getNewsType();
        $conditions = array();
        if ($caseType != 0) {
            $conditions['News.news_category_id'] =  $caseType;
        
        }
        $conditions['News.id >'] = $currentData['News']['id'];
        $data = $this->paginate('News', $conditions);
        $this->set('prevDetail', isset($data[0])?$data[0]:array());
    }

    private function _getNewsType()
    {
        $namedParams = $this->request->params['named']; 
        $caseType = 0;
        if (isset($namedParams['type'])
            && is_numeric($namedParams['type'])
        ) {
            $caseType = $namedParams['type'];
        }
        $this->set('caseType', $caseType);
        return $caseType;    
    }


    private function getNewss()
    {
        $data = array();
        $namedParams = $this->request->params['named']; 
        $caseType = 0;
        $conditions = array(
            'News.is_deleted' => 0
        );
        if (isset($namedParams['cate'])
            && is_numeric($namedParams['cate'])
        ) {
            $caseType = $namedParams['cate'];
            if ($caseType != 0) {
                $conditions['News.news_category_id'] =  $caseType;
            }
        }
        $data = $this->paginate('News', $conditions);
        $this->set('caseType', $caseType);
        return $data;
    }
}
